package com.ruoyi.business.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.TerminationAgreements;
import com.ruoyi.business.service.ITerminationAgreementsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 解约协议Controller
 *
 * @author ruoyi
 * @date 2025-02-13
 */
@RestController
@RequestMapping("/agreements")
public class TerminationAgreementsController extends BaseController
{
    @Autowired
    private ITerminationAgreementsService terminationAgreementsService;

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-查询解约协议列表
     */
    @RequiresPermissions("business:agreements:list")
    @GetMapping("/list")
    public TableDataInfo list(TerminationAgreements terminationAgreements)
    {
        startPage();
        List<TerminationAgreements> list = terminationAgreementsService.selectTerminationAgreementsList(terminationAgreements);
        return getDataTable(list);
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-导出解约协议列表
     */
    @RequiresPermissions("business:agreements:export")
    @Log(title = "解约协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TerminationAgreements terminationAgreements)
    {
        List<TerminationAgreements> list = terminationAgreementsService.selectTerminationAgreementsList(terminationAgreements);
        ExcelUtil<TerminationAgreements> util = new ExcelUtil<TerminationAgreements>(TerminationAgreements.class);
        util.exportExcel(response, list, "解约协议数据");
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-获取解约协议详细信息
     */
    @RequiresPermissions("business:agreements:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(terminationAgreementsService.selectTerminationAgreementsById(id));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-新增解约协议
     */
    @RequiresPermissions("business:agreements:add")
    @Log(title = "解约协议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TerminationAgreements terminationAgreements)
    {
        return toAjax(terminationAgreementsService.insertTerminationAgreements(terminationAgreements));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-修改解约协议
     */
    @RequiresPermissions("business:agreements:edit")
    @Log(title = "解约协议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TerminationAgreements terminationAgreements)
    {
        return toAjax(terminationAgreementsService.updateTerminationAgreements(terminationAgreements));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-删除解约协议
     */
    @RequiresPermissions("business:agreements:remove")
    @Log(title = "解约协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(terminationAgreementsService.deleteTerminationAgreementsByIds(ids));
    }

    // 提交解约申请
    @PostMapping("/apply")
    public AjaxResult applyTermination(@RequestBody TerminationAgreements application) {
        try {
            terminationAgreementsService.applyTermination(application);
            return AjaxResult.success("解约申请提交成功");
        } catch (Exception e) {
            return AjaxResult.error("解约申请提交失败：" + e.getMessage());
        }
    }

    // 查询解约申请状态
    @GetMapping("/status/{applicationId}")
    public AjaxResult getTerminationStatus(@PathVariable Long applicationId) {
        try {
            String status = terminationAgreementsService.getTerminationStatus(applicationId);
            return AjaxResult.success(status);
        } catch (Exception e) {
            return AjaxResult.error("查询解约状态失败：" + e.getMessage());
        }
    }
}